class ArrayCache {
    constructor(key, maxLength) {
        this.key = key;
        this.maxLength = maxLength ?? 3;
    }

    setOne(item) {
        let array = this.get();
        this.arrayRemove(array, item);

        array.unshift(item);
        if (array.length > this.maxLength) {
            array.pop();
        }
        localStorage.setItem(this.key, JSON.stringify(array));

        return array;
    }

    arrayRemove(array, item){
        let index = 0;
        while (index !== -1) {
            index = array.indexOf(item);
            if(index !== -1){
                array.splice(index, 1);
            }
        }
    }

    set(array) {
        if (array.length > this.maxLength) {
            array.splice(this.maxLength, array.length - this.maxLength)
        }
        localStorage.setItem(this.key, JSON.stringify(array));
    }

    get() {
        let array = [];
        try {
            let str = localStorage.getItem(this.key) || '[]';
            array = JSON.parse(str);
        } catch (e) {
        }
        return array;
    }


}